﻿using AutoMapper;
using MediatR;
using Traceability.App.API.Application.Command.Company;
using Traceability.App.API.DTOS;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Company
{
    /// <summary>
    /// 根据生产批次获取企业信息请求命令处理类
    /// </summary>
    public class GetCompanyHandler:IRequestHandler<GetCompanyCommand,ApiResult<List<CompanyDTO>>>
    {
        IBaseRepository<Company_Information> companyRep;
        IMapper mapper;

        public GetCompanyHandler(IBaseRepository<Company_Information> companyRep, IMapper mapper)
        {
            this.companyRep = companyRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<CompanyDTO>>> Handle(GetCompanyCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<CompanyDTO>> res = new ApiResult<List<CompanyDTO>>();

                var company=companyRep.GetAll().Where(x=>x.Production_Batch==request.production_Batch&&!x.IsDeleted);

                var model=mapper.Map<List<CompanyDTO>>(company);
                res.Msg = "根据生产批次获取企业信息";
                res.Code = ApiEnum.Success;
                res.Data = model;
                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
